package ReflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
     练习：通过反射实现如下操作：
         Stu s = new Stu();
         s.method1();
         s.method2("Jack");
         String ss = s.method3("Jack",30);
         System.out.println(ss);
         s.function();

 */
public class ReflectDemo7 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<?> aClass = Class.forName("ReflectDemo.Stu");

        Constructor<?> con = aClass.getConstructor();
        Object s = con.newInstance();

        Method method1 = aClass.getDeclaredMethod("method1");
        method1.setAccessible(true);
        method1.invoke(s);

        Method method2 = aClass.getDeclaredMethod("method2",String.class );
        method2.setAccessible(true);
        method2.invoke(s , "Jack");

        Method method3 = aClass.getDeclaredMethod("method3", String.class, int.class);
        method3.setAccessible(true);
        Object ss = method3.invoke(s, "Jack", 30);
        System.out.println(ss);

        Method function = aClass.getDeclaredMethod("function");
        function.setAccessible(true);
        function.invoke(s);


    }
}
